package com.chujie.chat.server;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NettyChannelMap {
    private static final Logger logger = LoggerFactory.getLogger(NettyServerHandler.class);

    private static Map<String, Channel> map = new ConcurrentHashMap<>();

    public static void add(String account, Channel channel) {
        map.put(account, channel);
    }
    public static boolean contain(String account) {
        return (map.get(account) != null);
    }
    public static Channel get(String clientId) {
        return map.get(clientId);
    }

    public static void remove(Channel channel) {
        for (Map.Entry entry : map.entrySet()) {
            if (entry.getValue() == channel) {
                String account = (String) entry.getKey();
                map.remove(account);
                System.out.println(account + " leave");
                logger.info(account + " leave");
                break;
            }
        }
    }
}